import pygame
import sys
from pygame.locals import *
import time
import pymunk as pm
import math
import os


def show_text(surface_handle, pos, text, color, font_bold=False, font_size=30, font_italic=False):


    cur_font = pygame.font.SysFont("arial", font_size)


    cur_font.set_bold(font_bold)

    cur_font.set_italic(font_italic)

    text_fmt = cur_font.render(text, 1, color)

    surface_handle.blit(text_fmt, pos)


def is_rect(pos, rect):
    x, y = pos
    rx, ry, rw, rh = rect
    if (rx <= x <= rw) and (ry <= y <= rh):
        return True
    return False


def to_pygame(p):
    """转换pymunk到pygame 坐标"""
    return int(p.x), int(-p.y + 600)


def vector(p0, p1):
    """向量差
    p0 = (xo,yo), p1 = (x1,y1)"""
    a = p1[0] - p0[0]
    b = p1[1] - p0[1]
    return (a, b)


def unit_vector(v):
    """单位向量
    v = (a,b)"""
    h = ((v[0] ** 2) + (v[1] ** 2)) ** 0.5
    if h == 0:
        h = 0.000000000000001
    ua = v[0] / h
    ub = v[1] / h
    return (ua, ub)


def distance(xo, yo, x, y):
    """两点间距离"""
    dx = x - xo
    dy = y - yo
    d = ((dx ** 2) + (dy ** 2)) ** 0.5
    return d


def load_music():
    song1 = os.path.join('music', 'ambient_red_savannah.wav')
    pygame.mixer.music.load(song1)
    pygame.mixer.music.play(-1)